<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>架构|代码整洁之道 |  麦田麦穗</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-代码整洁之道"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  架构|代码整洁之道
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/07/30/%E4%BB%A3%E7%A0%81%E6%95%B4%E6%B4%81%E4%B9%8B%E9%81%93/" class="article-date">
  <time datetime="2021-07-30T01:42:27.000Z" itemprop="datePublished">2021-07-30</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">1.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">6 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>软件质量不但依赖于架构及项目管理，还与代码质量紧密相关。</p>
<blockquote>
<p>代码质量与整洁度成正比，干净的代码即在质量上较为可靠，也为后期维护、升级奠定良好基础。</p>
</blockquote>
<p>另一种概念叫做<code>极限编程</code></p>
<h2 id="原则"><a href="#原则" class="headerlink" title="原则"></a>原则</h2><ul>
<li>SOLID</li>
<li>DRY</li>
<li>KISS</li>
</ul>
<h3 id="童子军军规"><a href="#童子军军规" class="headerlink" title="童子军军规"></a>童子军军规</h3><p><strong>让营地比你来时更干净</strong></p>
<span id="more"></span>

<h2 id="有意义的命名"><a href="#有意义的命名" class="headerlink" title="有意义的命名"></a>有意义的命名</h2><ul>
<li>名副其实，表达真实意义</li>
<li>不误导</li>
<li>做有意义的区分，避免 a、b、c</li>
<li>可读、可搜索</li>
</ul>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><ul>
<li>单一职责</li>
<li>行数 20-100</li>
</ul>
<h3 id="函数参数"><a href="#函数参数" class="headerlink" title="函数参数"></a>函数参数</h3><ul>
<li>0参数最佳，3个参数已经勉为其难</li>
<li>标识参数丑陋不堪，向参数传入布尔值骇人听闻</li>
<li>如果函数需要三个以上的参数，说明这些参数应该封装为类了</li>
</ul>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token class-name">Circle</span> <span class="token function">makeCircle</span><span class="token punctuation">(</span><span class="token keyword">double</span> x<span class="token punctuation">,</span><span class="token keyword">double</span> y<span class="token punctuation">,</span><span class="token keyword">double</span> radius<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Circle</span> <span class="token function">makeCircle</span><span class="token punctuation">(</span><span class="token class-name">Point</span> center<span class="token punctuation">,</span><span class="token keyword">double</span> radius<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<pre class="language-php" data-language="php"><code class="language-php"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Foo</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">bar</span><span class="token punctuation">(</span><span class="token variable">$flag</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token punctuation">&#125;</span>
</code></pre>

<h3 id="无副作用"><a href="#无副作用" class="headerlink" title="无副作用"></a>无副作用</h3><p>函数承诺只做一件事，实际上还做了其他的。</p>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserValidator</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">private</span> <span class="token class-name">Cryptographer</span> cryptographer<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">checkPassword</span><span class="token punctuation">(</span><span class="token class-name">String</span> userName<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token class-name">UserGateway</span><span class="token punctuation">.</span><span class="token function">findByName</span><span class="token punctuation">(</span>userName<span class="token punctuation">)</span> <span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>user <span class="token operator">!=</span> <span class="token class-name">User</span><span class="token punctuation">.</span>NULL<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">String</span> codedPhrase <span class="token operator">=</span> user<span class="token punctuation">.</span><span class="token function">getPhraseEncodedByPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> phrase <span class="token operator">=</span> cryptographer<span class="token punctuation">.</span>decrypt <span class="token punctuation">(</span>codedPhrase<span class="token punctuation">,</span>password <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">"Valid Password"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>phrase<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">Session</span><span class="token punctuation">.</span><span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//实际上还做了session 初始化的操作</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span></code></pre>

<p>要么抽离<code>Session.initialize()</code>,要么重命名为<code>checkPasswordAndInitializeSession</code> 不要给人误导。</p>
<h3 id="使用异常代替错误码"><a href="#使用异常代替错误码" class="headerlink" title="使用异常代替错误码"></a>使用异常代替错误码</h3><p>使用错误码就要定义错误码枚举，枚举类被大量导入调用。一旦增加或修改错误枚举，就要对所有引入的文件进行编译。</p>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">deletePage</span><span class="token punctuation">(</span>page<span class="token punctuation">)</span><span class="token operator">==</span>E_OK<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
       <span class="token keyword">if</span> <span class="token punctuation">(</span>registry<span class="token punctuation">.</span><span class="token function">deleteReference</span><span class="token punctuation">(</span>page<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token operator">==</span>E_OK<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
           <span class="token keyword">if</span> <span class="token punctuation">(</span>configKeys<span class="token punctuation">.</span><span class="token function">deleteKey</span><span class="token punctuation">(</span>page<span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">makeKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">==</span>E_OK<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
           logger<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"page deleted"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
           logger<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"configKey not deleted"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
           logger<span class="token punctuation">.</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"deleteReference from registry failed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
           logger<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"delete failed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span>
     <span class="token keyword">return</span> E_ERROR<span class="token punctuation">;</span>
   <span class="token punctuation">&#125;</span></code></pre>

<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
    <span class="token function">deletePage</span><span class="token punctuation">(</span>page<span class="token punctuation">)</span><span class="token punctuation">;</span>
    registry<span class="token punctuation">.</span><span class="token function">deleteReference</span><span class="token punctuation">(</span>page<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    configKeys<span class="token punctuation">.</span><span class="token function">deleteKey</span><span class="token punctuation">(</span>page<span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">makeKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    logger<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span></code></pre>

<h2 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h2><p>注释并不像辛德勒的名单。它们并不“纯然地好”。实际上，注释最多也就是一种必须的恶。若编程语言足够有表达力，或者我们长于用这些语言来表达意图，就不那么需要注释——也许根本不需要。</p>
<h3 id="注释掉的代码"><a href="#注释掉的代码" class="headerlink" title="注释掉的代码"></a>注释掉的代码</h3><p>20世纪60年代，曾经有那么一段时间，注释掉的代码可能有用。但我们已经拥有优良的源代码控制系统如此之久，这些系统可以为我们记住不要的代码。我们无需再用注释来标记，删掉即可，它们丢不了。我担保。</p>
<h2 id="格式"><a href="#格式" class="headerlink" title="格式"></a>格式</h2><h3 id="文件长度"><a href="#文件长度" class="headerlink" title="文件长度"></a>文件长度</h3><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210722110617.png" alt=""></p>
<p>200-500</p>
<h3 id="行字符数"><a href="#行字符数" class="headerlink" title="行字符数"></a>行字符数</h3><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210722111053.png" alt=""></p>
<p>上限120</p>
<h2 id="对象和数据结构"><a href="#对象和数据结构" class="headerlink" title="对象和数据结构"></a>对象和数据结构</h2><h3 id="德莫特定律"><a href="#德莫特定律" class="headerlink" title="德莫特定律"></a>德莫特定律</h3><p>著名的得墨式耳律（The Law of Demeter)认为，模块不应了解它所操作对象的内部形。如上节所见，对象隐藏数据，曝露操作。这意味着对象不应通过存取器曝露其内部结构<br>因为这样更像是曝露而非隐藏其内部结构。<br>更准确地说，得墨式耳律认为，类C的方法f只应该调用以下对象的方法：</p>
<ul>
<li>C</li>
<li>由f创建的对象；</li>
<li>作为参数传递给f的对象；</li>
<li>由C的实体变量持有的对象。</li>
</ul>
<h2 id="null"><a href="#null" class="headerlink" title="null"></a>null</h2><ul>
<li>别传null值</li>
<li>别返回null值</li>
</ul>
<h2 id="系统"><a href="#系统" class="headerlink" title="系统"></a>系统</h2><p>“复杂要人命。它消磨开发者的生命，让产品难以规划、构建和测试。”<br>——Ray Ozzie,微软公司首席技术官</p>
<h3 id="依赖注入"><a href="#依赖注入" class="headerlink" title="依赖注入"></a>依赖注入</h3><p>在依赖管理情景中，对象不应负责实体化对自身的依赖。反之，它应当将这份权责移交给其他“有权力”的机制，从而实现控制的反转。因为初始设置是一种全局问题，这种授权机制通常要么是main例程，要么是有特定目的的容器。</p>
<h3 id="扩容"><a href="#扩容" class="headerlink" title="扩容"></a>扩容</h3><blockquote>
<p>“一开始就做对系统”纯属神话。</p>
</blockquote>
<h3 id="代理"><a href="#代理" class="headerlink" title="代理"></a>代理</h3><p>AOP有时会与实现它的技术相混淆，例如方法拦截和通过代理做的“封包”。AOP系统的真正价值在于用简洁和模块化的方式指定系统行为。</p>
<h2 id="并发编程"><a href="#并发编程" class="headerlink" title="并发编程"></a>并发编程</h2><p>并发编程很难，非常难。如果你不那么细<br>心，就会搞出不堪入目的东西来。看看以下常见的迷思和误解：</p>
<ul>
<li><p>并发总能改进性能<br>并发有时能改进性能，但只在多个线程或处理器之间能分享大量等待时间的时候管用。<br>事情没那么简单。</p>
</li>
<li><p>编写并发程序无需修改设计<br>事实上，并发算法的设计有可能与单线程系统的设计极不相同。目的与时机的解藕往往对系统结构产生巨大影响。</p>
</li>
<li><p>在采用Web或EJB容器的时候，理解并发问题并不重要<br>实际上，你最好了解容器在做什么，了解如何对付本章后文将提到的并发更新、死锁等问题。</p>
</li>
</ul>
<p>下面是一些有关编写并发软件的中肯说法：</p>
<ul>
<li>并发会在性能和编写额外代码上增加一些开销；</li>
<li>正确的并发是复杂的，即便对于简单的问题也是如此；<br>并发缺陷并非总能重现，所以常被看做偶发事件而忽略，未被当做真的缺陷看待；</li>
<li>并发常常需要对设计策略的根本性修改。</li>
</ul>
<h2 id="味道建议"><a href="#味道建议" class="headerlink" title="味道建议"></a>味道建议</h2><h3 id="命名常量代替魔术数"><a href="#命名常量代替魔术数" class="headerlink" title="命名常量代替魔术数"></a>命名常量代替魔术数</h3><h3 id="准确"><a href="#准确" class="headerlink" title="准确"></a>准确</h3><ul>
<li>用浮点数表示货币几近于犯罪。</li>
<li>因为你不想做并发更新就避免使用锁和/或事务管理往好处说也是一种懒惰行为。在代码中做决定时，确认自己足够准确。</li>
<li>明确自己为何要这么做，如果遇到异常情况如何处理。</li>
<li>别懒得理会决定的准确性。如果你打算调用可能返回null的函数，确认自己检查了null值。</li>
<li>如果查询你认为是数据库中唯一的记录，确保代码检查不存在其他记录。</li>
<li>如果要处理货币数据，使用整数！并恰当地处理四舍五入。</li>
<li>如果可能有并发更新，确认你实现了某种锁定机制。</li>
<li>代码中的含糊和不准确要么是意见不同的结果，要么源于懒惰。无论原因是什么，都要消除。</li>
</ul>
<h3 id="返回异常"><a href="#返回异常" class="headerlink" title="返回异常"></a>返回异常</h3><pre class="language-php" data-language="php"><code class="language-php"><span class="token comment">/**
  * 
  * @param array $awbnos
  * @throws Exception|ConnectionTimeOutException
  * @return array Description
  */</span>
 <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">fetchTrace</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$awbnos</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword return-type">array</span>
 <span class="token punctuation">&#123;</span>
 <span class="token punctuation">&#125;</span></code></pre>

<p>php 利用 phpdoc 和 php7 特性能支持让调用者注意异常和返回值的正确处理。</p>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">preHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">&#123;</span>
<span class="token punctuation">&#125;</span></code></pre>

<p>java 函数定义语法天生支持。</p>
<h3 id="避免过多嵌套"><a href="#避免过多嵌套" class="headerlink" title="避免过多嵌套"></a>避免过多嵌套</h3><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210729173311.png" alt=""></p>
<h2 id="基本法"><a href="#基本法" class="headerlink" title="基本法"></a>基本法</h2><ul>
<li>单一职责，贯彻落实</li>
<li>短、少，类1000行，方法120行，参数3个，严格要求。</li>
<li>消灭mess,有1个就会有无数个</li>
<li>Later equals never 稍后等于永不，别等现在就去做</li>
<li>打磨，分解函数、修改名称、消除重复。缩短和重新安置方法。有拆散类。同时保持测试通过。<code>保持重构、热爱重构、注意单元测试回归测试</code></li>
</ul>
<h2 id="推荐阅读"><a href="#推荐阅读" class="headerlink" title="推荐阅读"></a>推荐阅读</h2><ol>
<li>《Clean Code》</li>
<li><a target="_blank" rel="noopener" href="http://kaelzhang81.github.io/2020/04/10/%E8%AF%91-%E8%AE%BE%E8%AE%A1%E9%AB%98%E8%B4%A8%E9%87%8F%E8%BD%AF%E4%BB%B6/">http://kaelzhang81.github.io/2020/04/10/译-设计高质量软件/</a></li>
</ol>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          Donate
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://visonforcoding.xyz/2021/07/30/%E4%BB%A3%E7%A0%81%E6%95%B4%E6%B4%81%E4%B9%8B%E9%81%93/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8A%80%E6%9C%AF%E5%B7%A5%E7%A8%8B/" rel="tag">技术工程</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/09/06/%E5%85%B3%E4%BA%8EOKR%E7%9A%84%E6%80%9D%E8%80%83/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            管理|关于OKR的思考
          
        </div>
      </a>
    
    
      <a href="/2021/07/27/php/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">利用PHPMD让你的php代码更干净更易维护</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> vison
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="麦田麦穗"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://shenyu-vip.lofter.com">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/null">Gallery</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>Buy me a cup of coffee~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpeg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wallet.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>